class Solution {
    public int longestSubarray(int[] nums) {
        int curMax = 0;
        for (int num : nums) {
            curMax = Math.max(curMax, num);
        }
        int lastIndex = -1;
        int maxLen = 0;
        int curLen = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == curMax) {
                if (lastIndex == -1) {
                    lastIndex = i;
                    curLen = 1;
                    maxLen = Math.max(maxLen, curLen);
                    continue;
                }
                if (i - lastIndex == 1) {
                    lastIndex = i;
                    curLen++;
                }
                maxLen = Math.max(maxLen, curLen);
                continue;
            }
            maxLen = Math.max(maxLen, curLen);
            lastIndex = -1;
        }
        return maxLen;
    }
}